package com.yige.web.controller

import com.mongodb.BasicDBObject
import com.mongodb.DBObject
import com.yige.common.DateUtil
import com.yige.common.ResultUtil
import com.yige.service.mongoService.MongoActionExpenditureService
import com.yige.service.mongoService.MongoExpenditureService
import com.yige.service.mongoService.MongoManagerService
import com.yige.service.mongoService.MongoTeacherService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.util.StringUtils
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.ResponseBody

/**
 * Created by sunhao on 2015/11/5 0005.
 */

@Controller
@RequestMapping("expend")
class ExpendController {

    @Autowired
    MongoExpenditureService expenditureService
    @Autowired
    MongoActionExpenditureService actionExpenditureService
    @Autowired
    MongoManagerService managerService
    @Autowired
    MongoTeacherService teacherService


    /*****************控制器模块*****************/

    /**
     * 显示支出列表
     * @param model
     * @return
     */
    @RequestMapping("list.do")
    def list(Model model){
        def expendList = expenditureService.findListDescendingByDate() as LinkedList
        def managerList = managerService.findList()
        def actionList = actionExpenditureService.findList()
        def teacherList = teacherService.findList()
        expendCount(model,expendList)
        model.addAttribute("teachers",teacherList)
        model.addAttribute("actions",actionList)
        model.addAttribute("managers",managerList)
        buildMap(model,expendList)
        return "expend/list"
    }

    /**
     * 多条件查询支出列表
     * @param model
     * @param startDate
     * @param endDate
     * @param manager
     * @param action
     * @return
     */
    @RequestMapping("find")
    def find(Model model, String startDate, String endDate, String manager, String action) {
        def query = new BasicDBObject()
        if (!StringUtils.isEmpty(startDate) && !StringUtils.isEmpty(endDate)) {
            query.append("date", [$gte: DateUtil.parseDate(startDate), $lte: DateUtil.parseDate(endDate)] as BasicDBObject)
        }
        if(!StringUtils.isEmpty(manager)){
            query.append("manager",manager)
        }
        if(!StringUtils.isEmpty(action)){
            def action_db = actionExpenditureService.findOneById(action)
            query.append("action",action_db.name)
        }
        def expendList = expenditureService.findListByQuery(query)
        def managerList = managerService.findList()
        def actionList = actionExpenditureService.findList()
        def teacherList = teacherService.findList()
        expendCount(model,expendList)
        model.addAttribute("teachers",teacherList)
        model.addAttribute("actions",actionList)
        model.addAttribute("managers",managerList)
        buildMap(model,expendList)
        return "expend/list"
    }

    /**
     * 添加支出
     * @param manager
     * @param action
     * @param description
     * @param pay
     * @param way
     * @return
     */
    @RequestMapping("addExpend")
    @ResponseBody
    def addExpend(String manager, String date, String action, String description,Double pay, Integer way,String teacher){
        def action_db = actionExpenditureService.findOneById(action)
        def expend = [_id:UUID.randomUUID().toString(),date:DateUtil.parseDate(date),manager:manager,description:description,
                      action:action_db.name,pay:pay,item:action_db.item,way:way,teacher:teacher]as BasicDBObject
        boolean flag = expenditureService.insertOne(expend)
        return ResultUtil.recallAjax(flag)
    }

    /**
     * 删除支出
     * @param _ids
     * @return
     */
    @RequestMapping("delExpend")
    @ResponseBody
    def delExpend(@RequestParam(value = '_ids') List<String> _ids){
        boolean flag = false
        _ids.each {_id->
            flag = expenditureService.deleteOne(_id)
        }
        return ResultUtil.recallAjax(flag)
    }

    /**
     * 修改支出
     * @param _id
     * @param manager
     * @param description
     * @param action
     * @param pay
     * @param way
     * @return
     */
    @RequestMapping("updateExpend")
    @ResponseBody
    def updateExpend(String _id,String manager, String date, String description,String action, Double pay, String way, String teacher){
        def action_db = actionExpenditureService.findOneById(action)
        def expend = [_id:_id,manager:manager,date:DateUtil.parseDate(date),description:description,teacher: teacher,
                      action:action_db.name,pay:pay as double,item:action_db.item,way:way]as BasicDBObject
        boolean flag = expenditureService.updateOne(_id,expend)
        return ResultUtil.recallAjax(flag)
    }



    /*****************方法模块*****************/

    def buildMap(Model model, List<DBObject> expendList){
        def expendMap = new HashMap()
        def list = new ArrayList()
        expendList.each {
            def manager = managerService.findOneById(it.manager)
            def way
            if(it.way == 0){
                way = "现金"
            }else {
                way = "转账"
            }
            expendMap.put("_id",it._id)
            expendMap.put("manager",manager.name)
            expendMap.put("pay",it.pay)
            expendMap.put("action",it.action)
            expendMap.put("description",it.description)
            expendMap.put("date",DateUtil.formatDate(it.get("date")))
            expendMap.put("way",way)
            list.add(expendMap)
            expendMap = new HashMap()
        }
        model.addAttribute("expends",list)
    }

    def expendCount(Model model, List<DBObject> expendList){
        def count = 0
        expendList.each{
            count = count + it.pay
        }
        model.addAttribute("expendCount",count)
    }

}
